<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>登录页面</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/static/img/favicon.ico}"/>
    <link rel="stylesheet" type="text/css"
          th:href="@{/plugins/bootstrap/css/bootstrap.min.css}">
    <script th:src="@{/js/jquery.min.js}"></script>
    <script th:src="@{/plugins/bootstrap/js/bootstrap.bundle.js}"></script>

    <link rel="stylesheet" type="text/css"
          th:href="@{/plugins/font-awesome/css/font-awesome.min.css}">


    <!-- 自定义样式 -->
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: #f8f9fa;
        }

        .form-login {
            background-color: #fff;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 0 10px #ccc;
            max-width: 500px;
            width: 100%;

            position: relative;
            top: 20px;
        }

        .form-login h3 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
            text-align: center;
        }

        .form-login .form-control {
            border-radius: 0px;
            padding-left: 40px;
            font-size: 16px;
        }

        .form-login .btn-primary {
            background-color: #007bff;
            border-color: #007bff;
            border-radius: 0px;
            font-size: 16px;
            width: 100%;
            margin-top: 20px;
            padding: 0px;
            line-height: 38px;
        }

        .form-login .form-check {
            margin-top: 10px;
            margin-bottom: 20px;
        }

        .form-login .form-check-label {
            font-size: 16px;
        }
        .form-group > label {
            display: inline-block;
            margin-top: 0.3rem;
            margin-bottom: 0.5rem;
        }

        body {
            background: url(/img/svg/bg.svg) no-repeat center center fixed;
            background-size: cover;
        }

        .form-group-item {
            position: relative;
        }

        .form-group-item i {
            width: 20px;
            height: 20px;
            position: absolute;
            line-height: 37px;
            left: 15px;
        }
        .email-code {
            display: flex;
            justify-content: flex-end;
        }
        .email-code > .form-group-item {
            flex-grow: 3;
        }
        #send_email_code_button {
            width: 120px;
            line-height: 38px;
            padding-left: 6px;
            cursor: pointer;
            background: none;
            border: none;
        }
    </style>
</head>
<body>

<div class="form-login">
    <h3>用户注册</h3>
    <form id="login-form">
        <div class="form-group">
            <label for="username">用户名：<span id="username-message"></span></label>
            <div class="form-group-item">
                <i class="fa fa-user"></i>
                <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名">
            </div>
        </div>
        <div class="form-group">
            <label for="password">密码：<span id="password-message"></span></label>
            <div class="form-group-item">
                <i class="fa fa-lock" aria-hidden="true"/></i>
                <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码">
            </div>
        </div>
        <div class="form-group">
            <label for="password">姓名：<span id="nickname-message"></span></label>
            <div class="form-group-item">
                <i class="fa fa-smile-o" aria-hidden="true" /></i>
                <input type="text" class="form-control" id="nickname" name="nickname" placeholder="请输入昵称">
            </div>
        </div>
        <div class="form-check">
            <input class="form-check-input" type="radio" name="type" id="user" value="0" checked>
            <label class="form-check-label" for="user">
                <i class="fa fa-users" aria-hidden="true" /></i>
                注册为学生
            </label>
        </div>
        <div class="form-check">
            <input class="form-check-input" type="radio" name="type" id="admin" value="1">
            <label class="form-check-label" for="admin">
                <i class="fa fa-user" aria-hidden="true" /></i>
                注册为老师
            </label>
        </div>
        <div class="form-group" id="student-id-box">
            <label for="password">学号：<span id="student-id-message"></span></label>
            <div class="form-group-item">
                <i class="fa fa-smile-o" aria-hidden="true" /></i>
                <input type="text" class="form-control" id="student_id" name="student_id" placeholder="请输入您的学号">
            </div>
        </div>
        <div class="form-group">
            <label for="password">邮箱：<span id="email-err-message"></span></label>

            <div class="form-group-item">
                <i class="fa fa-envelope-o" aria-hidden="true" /></i>
                <input type="text" class="form-control" id="email" name="email" placeholder="请输入邮箱">
            </div>
        </div>
        <div class="form-group email-code">
            <div class="form-group-item">
                <i class="fa fa-envelope-open-o" aria-hidden="true" /></i>
                <input type="text" class="form-control" name="email_code" placeholder="请输入验证码">
            </div>
            <button id="send_email_code_button">发送验证码</button>
        </div>

        <a href="/login.html" style="float: right;">已经有账号？</a>
        <button type="submit" class="btn btn-primary">注册</button>
    </form>
</div>
<script type="module">
    import {POST,GET} from "/js/request.js";
    import { formValidation,sendCodeWait,formVerify } from "/js/formUtils.js";
    let email_status = formValidation("#email",/^.+@.+\..+$/,"邮箱格式不正确","#email-err-message","√");
    let password_status =  formValidation("#password",/\s*.{4,}\s*/,"密码要求至少4位","#password-message","√");
    let username_status = formValidation("#username",/\s*.{4,}\s*/,"用户名要求至少4位","#username-message","√");
    let nickname_status = formValidation("#nickname",/\s*.+\s*/,"昵称不能为空","#nickname-message","√");
    let sendEmailCodeButton = $("#send_email_code_button");
    let startFormValid = formVerify(email_status,password_status,username_status,nickname_status);
    // 监听 Form 表单提交事件
    $("#login-form").on('submit', function (event) {
        if (! startFormValid()) return;
        let loginForm = {
            username: username_status.value,
            password: password_status.value,
            email: email_status.value,
            nickname: $("#nickname").val(),
            studentId: $("#student_id").val(),
            type: $('input[name="type"]:checked').val(),
            code: $("input[name='email_code']").val()
        }
        POST("/register", loginForm).then(result => {
            if (result.success) {
                window.location.href = "/manage.html";
            } else {
                alert("登录失败！" + result.message);
            }
        })
        // 阻止默认的表单提交行为
        event.preventDefault();
    });
    $("#send_email_code_button").click(function (event) {
        event.preventDefault();
        if (email_status.ok) {
            sendEmailCodeButton.attr("disabled",true)
            GET("/sendCode",{email:email_status.value}).then(result=>{
                if (result.success) {
                    sendCodeWait("#send_email_code_button",60)
                }else {
                    alert("发送验证码失败，请重试~");
                    sendEmailCodeButton.attr("disabled",false)
                }
            }).catch(error=>{
                sendEmailCodeButton.attr("disabled",false)
            })

        }else {
            alert(email_status.message)
        }
    })
    $('input[name="type"]').on('change', function() {
        if ($(this).is(':checked')) {
            let typeValue = $(this).val();
            // 当值为学生时显示填写学号
            let studentIdBox = $("#student-id-box");
            studentIdBox.attr("hidden",typeValue != 0)
        }
    })
</script>
</body>
</html>
